package com.jk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.TeamDao;
import com.jk.pojo.TeamBean;
import com.jk.utils.ReturnMsgUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jisiqi
 * @version 1.0
 * @description: TODO
 * @date 2022/10/17 16:43
 */
@Service
public class TeamService {

    @Autowired
    private TeamDao teamDao;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public Map getTeamList() {
        try {
            QueryWrapper<TeamBean> Wrapper = new QueryWrapper<TeamBean>();
            List<TeamBean> list = teamDao.selectList(Wrapper);
            System.out.println("MySQL中团队管理表数据"+list);
            if (list.size()>0){
                list.forEach(TeamBean ->{
                    elasticsearchRestTemplate.save(TeamBean);
                });
            }
            return  ReturnMsgUtils.returnMsg(200,"MySQL中团队管理表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器错误",null);
        }
    }

    public Map queryElasticsearchTeamList(TeamBean teamBean) {
        try {
            List<TeamBean> list = new ArrayList<>();
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //团队名称模糊查
            if (teamBean.getTeamName()!=null && teamBean.getTeamName()!=""){
                boolQueryBuilder.must(QueryBuilders.matchQuery("teamName",teamBean.getTeamName()));
            }
            //团队所属机构精确查
            if (teamBean.getTeamOrganization()!=null && teamBean.getTeamOrganization()!=-1){
                boolQueryBuilder.must(QueryBuilders.termQuery("teamOrganization",teamBean.getTeamOrganization()));
            }
            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(boolQueryBuilder)
                    .withHighlightFields(new HighlightBuilder.Field("teamName").preTags("<font color='red'>").postTags("</font>"))
                    .build();
            SearchHits<TeamBean> search = elasticsearchRestTemplate.search(build, TeamBean.class);
            search.forEach(hit ->{
                TeamBean content = hit.getContent();
                Map<String, List<String>> highlightFields = hit.getHighlightFields();
                hit.getContent().setTeamName(highlightFields.get("teamName")==null? hit.getContent().getTeamName():highlightFields.get("teamName").get(0));
                list.add(content);
            });
            return  ReturnMsgUtils.returnMsg(200,"es中团队管理表数据",list);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnMsgUtils.returnMsg(500,"服务器异常",null);
        }
    }

    public boolean deleteTeamId(Integer id) {
        try {
//            teamDao.deleteById(id);
            elasticsearchRestTemplate.delete(id.toString(), IndexCoordinates.of("team"));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateState(Integer id, Integer state) {
        try {
            TeamBean teamBean = new TeamBean();
            teamBean.setTeamId(id);
            teamBean.setTeamState(state);
            teamDao.updateById(teamBean);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
